// pages/module/earnForm/earnForm.js
import * as echarts from '../../../ec-canvas/echarts';
import regeneratorRuntime from '../../../utils/runtime.js';
import { getData, setData, clearData, dataKey } from '../../../utils/storage.js';
import shareFun from '../../../utils/shareFun.js';
import {
  dayForm,
  monthForm,
  commission_price_ordercnt,
  commission_items,
} from '../../../utils/imapi.js';
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    is_show:0,
    tabIndex:0,
    sliderLeft:150,
    sliderOffset:345,
    list1:1,
    list2:1,
    list3:1,
    list4:1,
    tabs:[
      {"id":1,"name":"按天展示","p":"","is_selected":1},
      {"id":2,"name":"按月展示","p":"JD","is_selected":0},
    ],
    ec: {
      lazyLoad: true
    },
    cur_index:0,
    btn_list:['今天','昨天','本月','上月'],
    order_cnt_ok:0,
    order_cnt_false:0,
    total_commission:0,
    lazy:false,
    recode_list:[
    
    ],
    page_index:0,
    page_size:10, // 默认分页
	
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function (options) {
    shareFun.hidenShareMenu();
    const dayData = await dayForm({
      // member_id:'1272073265006776320'
      page_size:30,
    })
    this.daySeries = [];
    for(var i = 0; i < dayData.data.length; i++){

      // 07-10,07-11
      var month_day_value= dayData.data[i].date_value.substring(5)

      var arr = [month_day_value,dayData.data[i].commission]
      this.daySeries.push(arr)
    }


    const monthData = await monthForm({
      // member_id:'1272073265006776320'
      page_size:12,
    })
    this.monthSeries = [];
    for(var i = 0; i < monthData.data.length; i++){

      // 5月  6月
      //var month_value= monthData.data[i].date_value.substring(5)
      var arr1 = [monthData.data[i].date_value,monthData.data[i].commission]
      this.monthSeries.push(arr1)
    }
	//console.log(this.monthSeries);
    this.ecComponent = this.selectComponent('#mychart-dom-line');
	
    this.init()
    this.setData({
      tabIndex:0  //一定要先设置到 按月展示， 否则只能显示一个
    })

    this.ecComponent1 = this.selectComponent('#mychart-dom-line1');
	//console.log(this.ecComponent1);
    this.init1()

     var curTime = new Date()
     var timestamp = Date.parse(curTime);
	 var fullyear=curTime.getFullYear();
     var mouth = (curTime.getMonth()+1);
     var day = curTime.getDate();
     var today =fullyear+'-'+ (mouth > 9 ? mouth : ('0' + mouth)) + '-' + (day > 9 ? day : ('0' + day));
     console.log(today)
	 // 今天
	 let data=  await commission_price_ordercnt({day_month:1,time_str:today})
	 
	 this.setData({
	    total_commission:data.data.commission,
		order_cnt_ok:data.data.order_cnt_ok,
		order_cnt_false:data.data.order_cnt_false,
	 })
	 
   this.loadOrderItems(1)

  },
  init(e){
	 // console.log('点击了日');
    var that = this
    this.ecComponent.init((canvas, width, height, dpr) => {
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr 
      });
     // console.log(height)
      that.setOption(chart,that.daySeries);

      this.chart = chart;

      this.setData({
        isLoaded: true,
        isDisposed: false
      });
      return chart;
    });
  },
  init1(e){
    var that = this
    this.ecComponent1.init((canvas, width, height, dpr) => {
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr 
      });
    //  console.log(that.monthSeries);
      that.setOption(chart,that.monthSeries);
  
      this.chart = chart;
  
      this.setData({
        isLoaded: true,
        isDisposed: false
      });
      return chart;
    });
  },
  setOption(chart,sdata) {
	  // console.log(chart);
	  // console.log(sdata);
    var option = {
      color: ["#F95A4B"],
      grid: {
        left:10,
        right:20,
        top: 10,
        bottom:10,
        containLabel: true
      },
      tooltip: {
        show: true,
        trigger: 'axis'
      },
      xAxis: {
        type: 'category',
        min: 'dataMin',
        max: 'dataMax',
        boundaryGap: false,
        nameTextStyle:{
          fontFamily:'PingFangSC-Regular',
        },
        axisTick:{
          lineStyle:{
            color:'#F6F5F6'
          }
        },
        axisLine:{
          lineStyle:{
            color:'#9D9CA3'
          }
        },
        splitLine:{
          show:true,
          lineStyle:{
            color:'#F6F5F6'
          }
        },
      },
      yAxis: {
        type: 'value',
        min: 'dataMin',
        max: 'dataMax',
        nameTextStyle:{
          fontFamily:'PingFangSC-Regular',
        },
        axisTick:{
          lineStyle:{
            color:'#F6F5F6'
          }
        },
        axisLine:{
          lineStyle:{
            color:'#9D9CA3'
          }
        },
        splitLine:{
          show:true,
          lineStyle:{
            color:'#F6F5F6'
          }
        },
      },
      dataZoom: [
        {
          type: 'inside',
          xAxisIndex: [0],
          start: 100,
          end: 40,
        },
      ],
      series: [{
        name: 'A',
        type: 'line',
        smooth: true,
        data: sdata
      }]
    };
    chart.setOption(option);
  },
  showOrderItems:async function(e){ //切换有效无效订单
    console.log('--有效还是无效订单')
   
    let status=e.currentTarget.dataset.status
    let index=this.data.cur_index
    this.setData({
      recode_list:[],
      status:status
    })

    this.loadOrderItems(1)



  },

  async loadOrderItems(page_index){  // 加载 订单数据

    let index =this.data.cur_index
    console.log('-----index:',index)

    var curTime = new Date()
    var timestamp = Date.parse(curTime);
    var fullyear=curTime.getFullYear();
    var mouth = (curTime.getMonth()+1);
    var day = curTime.getDate();
    var today =fullyear+'-'+ (mouth > 9 ? mouth : ('0' + mouth)) + '-' + (day > 9 ? day : ('0' + day));
    console.log(today)  //今天
    var today1 =fullyear+'-'+ (mouth > 9 ? mouth : ('0' + mouth)) + '-' + (day > 9 ? Number(day)-1 : ('0' + String(Number(day)-1)));
    console.log(today1); //昨天
    var today2 =fullyear+'-'+ (mouth > 9 ? mouth : ('0' + mouth)) + '-' + '01';
    console.log(today2);  //这月
    var today3 =fullyear+'-'+ (mouth > 9 ? mouth : ('0' + mouth)) + '-' + '01';
    console.log(today3);  //上月开始日期
    let status=this.data.status
    if(index==0){
      // 今天
       let {data}= await commission_items({page_index:page_index,page_size:this.data.page_size,time_str:today,day_month:1,status:status});
       this.setData({
        recode_list:this.data.recode_list.concat(data),
        page_index:page_index
       })

    }else if(index==1){
      //昨天

      let {data}= await commission_items({page_index:page_index,page_size:this.data.page_size,time_str:today1,day_month:1,status:status});
      this.setData({
        recode_list:this.data.recode_list.concat(data),
        page_index:page_index
      })
    }else if (index==2){
      // 本月
     let {data}= await commission_items({page_index:page_index,page_size:this.data.page_size,time_str:today2,day_month:2,status:status});
	  console.log(data);
      this.setData({
        recode_list:this.data.recode_list.concat(data),
        page_index:page_index
      })
    }else if(index==3){
      // 上月
      let {data}= await commission_items({page_index:page_index,page_size:this.data.page_size,time_str:today3,day_month:2,status:status});
      console.log(data)
      this.setData({
        recode_list:this.data.recode_list.concat(data),
        page_index:page_index
      })
	    console.log(this.data.recode_list);
    }


  },
  async changeTab(e){
    var index = e.currentTarget.dataset.index;
    this.setData({
      cur_index:index,
      recode_list:[],
      status:''
    })
	     var curTime = new Date()
		 var timestamp = Date.parse(curTime);
		 var fullyear=curTime.getFullYear();
		 var mouth = (curTime.getMonth()+1);
		 var day = curTime.getDate();
		 var today =fullyear+'-'+ (mouth > 9 ? mouth : ('0' + mouth)) + '-' + (day > 9 ? day : ('0' + day));
		 console.log(today)  //今天
		 var today1 =fullyear+'-'+ (mouth > 9 ? mouth : ('0' + mouth)) + '-' + (day > 9 ? Number(day)-1 : ('0' + String(Number(day)-1)));
		 console.log(today1); //昨天
		 var today2 =fullyear+'-'+ (mouth > 9 ? mouth : ('0' + mouth)) + '-' + '01';
     console.log(today2);  //这月
     
     mouth = mouth-1 //但是只支持到 非跨年

		 var today3 =fullyear+'-'+ (mouth > 9 ? Number(mouth) : ('0' + String(Number(mouth)))) + '-' + '01';
		 console.log(today3);  //上月开始日期

    if(index==0){
      // 今天
       let data=  await commission_price_ordercnt({day_month:1,time_str:today})

       let date= await commission_items({page_index:1,page_size:this.data.page_size,time_str:today,day_month:1,status:''});
       this.setData({
        order_cnt_ok:data.data.order_cnt_ok,
        order_cnt_false:data.data.order_cnt_false,
        total_commission:data.data.commission,
        recode_list:date.data,
        list2:1,
        list3:1,
        list4:1,
       })

    }else if(index==1){
      //昨天
      let data=  await commission_price_ordercnt({day_month:1,time_str:today1})
     // let date=  await orderList({page_index:1,page_size:10,start_time:today1});

      let date= await commission_items({page_index:1,page_size:this.data.page_size,time_str:today1,day_month:1,status:''});
      this.setData({
        order_cnt_ok:data.data.order_cnt_ok,
        order_cnt_false:data.data.order_cnt_false,
        total_commission:data.data.commission,
        recode_list:date.data,
        list1:1,
        list3:1,
        list4:1,
      })
    }else if (index==2){
      // 本月
      let data=  await commission_price_ordercnt({day_month:2,time_str:today2})
     // let date=  await orderList({page_index:1,page_size:10,start_time:today2});
     let date= await commission_items({page_index:1,page_size:this.data.page_size,time_str:today2,day_month:2,status:''});
	  console.log(data);
      this.setData({
        order_cnt_ok:data.data.order_cnt_ok,
        order_cnt_false:data.data.order_cnt_false,
        total_commission:data.data.commission,
		recode_list:date.data,
		list1:1,
		list2:1,
		list4:1,
      })
    }else if(index==3){
      // 上月
      let data= await commission_price_ordercnt({day_month:2,time_str:today3})
      //let date=  await orderList({page_index:1,page_size:10,start_time:today3,end_time:today2});
      let date= await commission_items({page_index:1,page_size:this.data.page_size,time_str:today3,day_month:2,status:''});
      this.setData({
        order_cnt_ok:data.data.order_cnt_ok,
        order_cnt_false:data.data.order_cnt_false,
        total_commission:data.data.commission,
		recode_list:date.data,
		list1:1,
		list2:1,
		list3:1,
      })
	  console.log(this.data.recode_list);
    }
    

  },
  async onTabClick(event) {
    var index = event.detail.index;
    this.setData({
      tabIndex:index
    })
	console.log(this.data.tabIndex);
	if(this.data.tabIndex==0){
		this.init();
	}else{
		this.init1();
	}
	
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    setTimeout(() => {
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
    }, 500);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom:async function () {
    this.setData({
      lazy:true
    })
   
   this.loadOrderItems(this.data.page_index+1)

		setTimeout(res=>{
			this.setData({
				lazy:false
			})
		},1000)
	
	
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})